#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,B=600,M=N/B+5,K=(1<<15)+5;
bool stt;
int n,q,c,a[N],fa[N],fa1[N];
vector<int>son[N];
int top[N],sz[N],tofa[N],pos[N],tot,fp[N],tos[M][N],sum[M][K],p[21][N],pv[21][N];
void dfs1(int x,int prt)
{
	sz[x]=1;
	for(int i=1;i<=20;i++)p[i][x]=p[i-1][p[i-1][x]],pv[i][x]=pv[i-1][x]|(pv[i-1][p[i-1][x]]);
	for(auto y:son[x])if(y!=prt)p[0][y]=x,pv[0][y]=(1<<a[x]),dfs1(y,x),sz[x]+=sz[y];
	if(sz[x]>=B || x==1)sz[x]=0,top[x]=x,tot++,pos[x]=tot,fp[tot]=x;
}
int nrt;
bool ins[N],ex[N];
int nsum,nows;
int ct;
int dfs4(int x,int prt)
{
	if(pos[x])return sum[pos[x]][nows];
	int num=1;
	for(auto y:son[x])if(y!=prt && ((nows>>a[y])&1))num+=dfs4(y,x);
	return num;
}
bool edd;
int main()
{
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	cerr<<((&stt)-(&edd))/1024.0/1024<<"\n";
	scanf("%d %d %d",&n,&q,&c);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=2;i<=n;i++)scanf("%d",&fa[i]),son[fa[i]].push_back(i);
	dfs1(1,0);
	for(int i=1;i<=tot;i++)
	{
		tos[i][fp[i]]=1<<a[fp[i]],nrt=i;
		for(int j=1;j<=n;j++)ins[j]=0;
		ins[fp[i]]=1;queue<int>q;q.push(fp[i]);
		while(!q.empty())
		{
			int x=q.front();q.pop();
			for(auto y:son[x])if(!ins[y])ins[y]=1,q.push(y),tos[i][y]=tos[i][x]|(1<<a[y]);
		}
		for(int j=1;j<=n;j++)if(ins[j])sum[i][tos[i][j]]++;
		for(int j=0;j<c;j++)for(int k=0;k<(1<<c);k++)if((k>>j)&1)sum[i][k]+=sum[i][k^(1<<j)];
	}
	while(q--)
	{
		int x,s;
		scanf("%d %d",&s,&x);
		ct++;
		//if(ct==199278)
		//{
		//	cerr<<s<<" "<<x<<" "<<a[x]<<endl;
		//	cerr<<p[0][x]<<" "<<a[p[0][x]]<<endl;
		//}
		if((s>>a[x])&1^1){printf("1\n");continue;}
		for(int i=20;i>=0;i--)if(p[i][x] && (s|pv[i][x])==s)x=p[i][x];
		//if(ct==199278)
		//{
		//	cerr<<x<<" "<<s<<endl;
		//	for(auto i:son[x])cerr<<i<<" "<<a[i]<<endl;
		//}
		nows=s;
		printf("%d\n",dfs4(x,0));
	}
	return 0;
}